import time
from new_test_framework.utils import tdLog, tdSql, sc, clusterComCheck, clusterComCheck


class TestRedistributeVgroupReplica3V1Leader:

    def setup_class(cls):
        tdLog.debug(f"start to execute {__file__}")

    def test_redistribute_vgroup_replica3_v1_leader(self):
        """RDST: replica-3 leader

        1. Start a 4-node cluster with dnode1 configured as supportVnodes=0
        2. Create database d1 (1 vgroup, 3 replicas) and write data
        3. Execute illegal REDISTRIBUTE VGROUP commands (expected to fail due to insufficient eligible dnodes)
        4. Add dnode5 to the cluster
        5. Identify the follower vnode and keep it unchanged
        6. Move the leader vnode among the remaining three dnodes five times; after each move, check vnode distribution and data integrity

        Catalog:
            - Database:Sync

        Since: v3.0.0.0

        Labels: common,ci

        Jira: None

        History:
            - 2025-5-6 Simon Guan Migrated from tsim/dnode/redistribute_vgroup_replica3_v1_leader.sim

        """

        clusterComCheck.checkDnodes(5)
        tdSql.execute(f"alter dnode 1 'supportVnodes' '0'")
        clusterComCheck.checkDnodeSupportVnodes(1, 0)

        sc.dnodeStop(5)
        clusterComCheck.checkDnodes(4)
        tdSql.execute(f"create user u1 pass 'taosdata'")

        tdLog.info(f"=============== step1 create dnode2")

        tdSql.query(f"select * from information_schema.ins_dnodes")
        tdSql.checkRows(5)
        tdSql.checkKeyData(1, 4, "ready")
        tdSql.checkKeyData(2, 4, "ready")
        tdSql.checkKeyData(3, 4, "ready")
        tdSql.checkKeyData(4, 4, "ready")

        tdLog.info(f"=============== step2: create db")
        tdSql.execute(f"create database d1 vgroups 1 replica 3")
        clusterComCheck.checkDbReady("d1")

        sc.dnodeStart(5)
        clusterComCheck.checkDnodes(5)

        tdSql.query(f"select * from information_schema.ins_dnodes")
        tdSql.checkRows(5)
        tdSql.checkKeyData(1, 4, "ready")
        tdSql.checkKeyData(2, 4, "ready")
        tdSql.checkKeyData(3, 4, "ready")
        tdSql.checkKeyData(4, 4, "ready")
        tdSql.checkKeyData(5, 4, "ready")

        tdLog.info(f"=============== step3: create database")
        leaderExist = 0
        leaderVnode = 0
        follower1 = 0
        follower2 = 0

        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )
        tdSql.checkRows(1)

        if tdSql.getData(0, 4) == "leader":
            leaderExist = 1
            leaderVnode = 2
            follower1 = 3
            follower2 = 4

        if tdSql.getData(0, 7) == "leader":
            leaderExist = 1
            leaderVnode = 3
            follower1 = 2
            follower2 = 4

        if tdSql.getData(0, 10) == "leader":
            leaderExist = 1
            leaderVnode = 4
            follower1 = 2
            follower2 = 3

        tdLog.info(f"leader {leaderVnode}")
        tdLog.info(f"follower1 {follower1}")
        tdLog.info(f"follower2 {follower2}")

        tdSql.execute(f"use d1")
        tdSql.execute(f"create table d1.st (ts timestamp, i int) tags (j int)")
        tdSql.execute(f"create table d1.c1 using st tags(1)")
        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)

        tdLog.info(f"=============== step40: move leader")
        tdLog.info(f"redistribute vgroup 2 dnode {follower1} dnode {follower2} dnode 5")
        tdSql.execute(
            f"redistribute vgroup 2 dnode {follower1} dnode {follower2} dnode 5"
        )
        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )

        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)

        tdLog.info(f"=============== step41:")
        tdLog.info(
            f"redistribute vgroup 2 dnode {leaderVnode} dnode {follower2} dnode 5"
        )
        tdSql.execute(
            f"redistribute vgroup 2 dnode {leaderVnode} dnode {follower2} dnode 5"
        )
        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )

        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)

        tdLog.info(f"=============== step42:")
        tdLog.info(
            f"redistribute vgroup 2 dnode {follower1} dnode 5 dnode {leaderVnode}"
        )
        tdSql.execute(
            f"redistribute vgroup 2 dnode {follower1} dnode 5 dnode {leaderVnode}"
        )
        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )

        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)

        tdLog.info(f"=============== step43:")
        tdLog.info(
            f"redistribute vgroup 2 dnode {follower1} dnode {follower2} dnode {leaderVnode}"
        )
        tdSql.execute(
            f"redistribute vgroup 2 dnode {follower1} dnode {follower2} dnode {leaderVnode}"
        )
        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )

        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)

        tdLog.info(f"=============== step44:")
        tdLog.info(f"redistribute vgroup 2 dnode {follower2} dnode 5 dnode {follower1}")
        tdSql.execute(
            f"redistribute vgroup 2 dnode {follower2} dnode 5 dnode {follower1}"
        )
        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )

        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)

        tdLog.info(f"=============== step45:")
        tdLog.info(
            f"redistribute vgroup 2 dnode {leaderVnode} dnode {follower2} dnode 5"
        )
        tdSql.execute(
            f"redistribute vgroup 2 dnode {leaderVnode} dnode {follower2} dnode 5"
        )
        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )

        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)

        tdLog.info(f"=============== step46:")
        tdLog.info(
            f"redistribute vgroup 2 dnode {leaderVnode} dnode {follower1} dnode 5"
        )
        tdSql.execute(
            f"redistribute vgroup 2 dnode {leaderVnode} dnode {follower1} dnode 5"
        )
        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )

        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)

        tdLog.info(f"=============== step47:")
        tdLog.info(f"redistribute vgroup 2 dnode {follower1} dnode 5 dnode {follower2}")
        tdSql.execute(
            f"redistribute vgroup 2 dnode {follower1} dnode 5 dnode {follower2}"
        )
        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )

        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)

        tdLog.info(f"=============== step48:")
        tdLog.info(
            f"redistribute vgroup 2 dnode {leaderVnode} dnode 5 dnode {follower2}"
        )
        tdSql.execute(
            f"redistribute vgroup 2 dnode {leaderVnode} dnode 5 dnode {follower2}"
        )
        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )

        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)

        tdLog.info(f"=============== step49:")
        tdLog.info(
            f"redistribute vgroup 2 dnode {leaderVnode} dnode 5 dnode {follower2}"
        )
        tdSql.execute(
            f"redistribute vgroup 2 dnode {leaderVnode} dnode 5 dnode {follower2}"
        )
        tdSql.query(f"show d1.vgroups")
        tdLog.info(
            f"===> {tdSql.getData(0,0)} {tdSql.getData(0,1)} {tdSql.getData(0,2)} {tdSql.getData(0,3)} {tdSql.getData(0,4)} {tdSql.getData(0,5)} {tdSql.getData(0,6)} {tdSql.getData(0,7)} {tdSql.getData(0,8)} {tdSql.getData(0,9)}"
        )

        tdSql.query(f"show d1.tables")
        tdSql.checkRows(1)


# system sh/exec.sh -n dnode1 -s stop -x SIGINT
# system sh/exec.sh -n dnode2 -s stop -x SIGINT
# system sh/exec.sh -n dnode3 -s stop -x SIGINT
# system sh/exec.sh -n dnode4 -s stop -x SIGINT
# system sh/exec.sh -n dnode5 -s stop -x SIGINT
